<?php

namespace app\common\business;

use app\common\lib\Curl;
use app\common\lib\Status;
use app\common\lib\Str;
use app\common\lib\Time;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\facade\Cache;

class UserAssociation
{

    public $model = null;

    public function __construct()
    {
        $this->model = new \app\common\model\mysql\UserAssociation();
    }

    public function saveData($user_id, $data)
    {
        $user = (new User())->getNormalUserById($user_id);
        if (!$user) {
            throw new Exception("不存在该用户");
        }

        //完善关联企业信息
        $info = self::getNormalSimpleInfoByField('user_id',$user_id);
        if (!$info) {
            //新增
            $userAssociationData = [
                'user_id' => $user_id,
                'name' => $data['name'],
                'position' => $data['position'],
                'business_license' => $data['business_license'],
                'status' => config('status.mysql.table_normal'),
                'audit' => Status::getKeyByValue('user.association_status','待审核'),
                'create_time' => time()
            ];

            try {
                $res = $this->model->save($userAssociationData);
            } catch (\Exception $e) {
                throw new Exception("数据库内部异常");
            }

            //$id = $this->model->id;
        }else{
            //更新
            $id = $info['id'];
            $data['audit'] = Status::getKeyByValue('user.association_status','待审核');
            try {
                $res = $this->model->updateById($id,$data);
            } catch (\Exception $e) {
                throw new Exception("数据库内部异常");
            }
        }

        return $res;
    }

    //判断关联企业审核状态通过
    public function checkAuditYes($audit){
        $flag = false;

        $yes = Status::getKeyByValue('user.association_status','审核通过');
        $audit == $yes && $flag = true;
        return $flag;
    }

    /**
     *
     * @param $field
     * @param $value
     * @return array
     */
    public function getNormalByField($field,$value): array
    {
        $data = $this->model->getNormalByField($field, $value);
        if (!$data || $data->status != config("status.mysql.table_normal")) {
            return [];
        }
        return $data->toArray();
    }

    public function getNormalSimpleInfoByField($field,$value)
    {
        $data = $this->model->getNormalByField($field,$value);
        if (!$data || $data->status != config("status.mysql.table_normal")) {
            return [];
        }
        return $data->toArray();
    }

    public function resetData($data){
        if(!empty($data)){
            $data['audit_text']=isset($data['audit'])?config('user.association_status')[$data['audit']]:'';
        }
        return $data;
    }

    /**
     * 审核企业信息
     * @param $uid
     * @param $audit
     * @param $remark
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function audit($uid,$audit,$remark){
        $row=$this->model->getNormalByField('user_id',$uid);
        if(empty($row)){
            throw new Exception('关联信息不存在');
        }
        return $this->model->where(['user_id'=>$uid])->save(['audit'=>$audit,'remark'=>$remark]);
    }

    /**
     * @param $where
     * @param $page
     * @param $limit
     * @param $field
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getListByWhere($where,$page,$limit,$field='*'){
        $where[] = ['status','=',config("status.mysql.table_normal")];
        if ($page!=0 && $limit !=0){
            $list = $this->model->getPageListByWhere($where,$page,$limit,$field);
        }else{
            $list = $this->model->getNormalListByWhere($where,$field);
        }
        if($list->isEmpty()){
            return [];
        }
        return $list->toArray();
    }
}
